package houserent.view;

import houserent.domain.House;
import houserent.service.HouseService;
import houserent.utils.Utility;

/**
 * @author 杨森
 * @date 2022年10月11日 14:54
 * 显示界面
 * 接收用户的输入
 * 调用HouseService完成对房屋信息的各种操作
 */
public class HouseView {
    private boolean loop = true;//控制显示菜单
    private char key = ' ';//接收用户选择
    private HouseService houseService = new HouseService(10);//设置数组的大小为10

    //    编写listHouse()显示房屋列表
    public void listHouses() {
        System.out.println("\t=============房屋列表=============");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t状态(未出租/已出租)");
        House[] houses = houseService.list();//得到所有房屋信息
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] == null) {
                break;
            }
            System.out.println(houses[i]);
        }
        System.out.println("=============房屋列表显示完毕=============");

    }

    //    编写addHouse（）接收输入，创建House对象，调用add方法
    public void addHouse() {
        System.out.println("\t=============添加房屋=============");
        System.out.print("姓名: ");
        String name = Utility.readString(8);
        System.out.print("电话: ");
        String phone = Utility.readString(12);
        System.out.print("地址: ");
        String address = Utility.readString(16);
        System.out.print("月租: ");
        int rent = Utility.readInt();
        System.out.print("状态: ");
        String state = Utility.readString(3);
//        创建一个新的House对象,id是系统分配的，
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("\t=============添加房屋成功=============");
        } else {
            System.out.println("\t=============添加房屋失败=============");
        }
    }

    //    编写delete（） 接收输入的id,调用Service的del方法
    public void delHouse() {
        System.out.println("\t=============删除房屋信息=============");
        System.out.println("请输入待删除房屋的编号（-1退出）");
        int delId = Utility.readInt();
        if (delId == -1) {
            System.out.println("\t=============放弃删除房屋信息=============");
            return;
        }
        System.out.println("请确认是否删除（Y/N）,小心选择");
//        该方法本身即有循环判断的逻辑，必须输出Y/N
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            if (houseService.del(delId)) {
                System.out.println("\t=============删除房屋信息成功=============");
            } else {
                System.out.println("\t=============房屋编号不存在，删除失败=============");
            }
        } else {
            System.out.println("\t=============放弃删除房屋信息=============");
        }
    }

    //    完成退出确认
    public void exit() {
        char c = Utility.readConfirmSelection();
        if (c == 'Y') {
            loop = false;
        }
    }

    //    完成查找
    public void findHouse() {
        System.out.println("\t=============查找房屋信息=============");
        System.out.println("请输入需要查找的房屋的ID（-1退出）");
        int findId = Utility.readInt();
        House house = houseService.findById(findId);
        if (house != null) {
            System.out.println(house);
        } else {
            System.out.println("\t=============查找的房屋信息不存在=============");
        }
    }

    //    根据id修改房屋信息
    public void update() {
        System.out.println("\t=============修改房屋信息=============");
        System.out.println("请选择待修改房屋编号（-1表示退出）");
        int updateId = Utility.readInt();
        if (updateId == -1) {
            System.out.println("\t=============你放弃修改房屋信息=============");
            return;
        }

//        根据输入得到updateId,查找对象
//        返回的是引用类型【数组的元素】
//        修改house.set会修改HouseService中houses数组的元素
        House house = houseService.findById(updateId);
        if (house == null) {
            System.out.println("\t=============修改房屋信息编号不存在=============");
            return;
        }
        System.out.print("姓名（" + house.getName() + "):");
        String name = Utility.readString(8, "");//这里如果用户直接回车表示不修改该信息，默认“
        if (!"".equals(name)) {//修改
            house.setName(name);
        }
        System.out.print("电话(" + house.getPhone() + "):");
        String phone = Utility.readString(12,"");
        if (!"".equals(phone)) {
            house.setPhone(phone);

        }
        System.out.print("地址(" + house.getAddress() + "):");
        String address = Utility.readString(18,"");
        if (!"".equals(address)) {
            house.setAddress(address);
        }
        System.out.print("租金(" + house.getRent() + "):");
        int rent = Utility.readInt(-1);
        if (!"".equals(rent)) {
            house.setRent(rent);
        }
        System.out.print("状态(" + house.getAddress() + "):");
        String state = Utility.readString(3,"");
        if (!"".equals(state)) {
            house.setState(state);
        }
        System.out.println("\t============修改房屋信息成功=============");
    }


    //    显示主菜单
    public void mainMenu() {
        do {
            System.out.println("-------------房屋出租系统-------------");
            System.out.println("\t\t\t1 新 增 房 源");
            System.out.println("\t\t\t2 查 找 房 屋");
            System.out.println("\t\t\t3 删 除 房 屋");
            System.out.println("\t\t\t4 修 改 房 屋 信 息");
            System.out.println("\t\t\t5 房 屋 列 表");
            System.out.println("\t\t\t6 退\t出");
            System.out.println("请输入你的选择（1-6）:");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    addHouse();
                    break;
                case '2':
//                    System.out.println("查 找");
                    findHouse();
                    break;
                case '3':
                    delHouse();
                    break;
                case '4':
//                    System.out.println("修 改");
                    update();
                    break;
                case '5':
                    listHouses();
                    break;
                case '6':
//                    System.out.println("退 出");
                    exit();
                    break;
            }


        } while (loop);
    }

}
